/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hu.bme.j2ee.v37zen.util;



import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.codec.binary.Base64;

/**
 *
 * @author Kiss Dániel
 */
public class PasswordDigest {
    
    public static String encodePassword(String passwordAsString) throws NoSuchAlgorithmException, UnsupportedEncodingException{

        byte[] bytesOfMessage = passwordAsString.getBytes("UTF-8");

        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] thedigest = md.digest(bytesOfMessage);
        return Base64.encodeBase64String(thedigest);
        
    }
    
    
    public static String encodePasswordUnsafe(String passwordAsString){
        try {
            return encodePassword(passwordAsString);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(PasswordDigest.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(PasswordDigest.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
}
